package com.qust.EnDecryption.util.net.status;

import com.alibaba.fastjson2.JSON;
import com.qust.EnDecryption.util.net.HState;
import com.qust.EnDecryption.util.net.IRequest;
import com.qust.EnDecryption.util.net.NetTool;
import com.qust.agreement.result.Result;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public abstract class HttpState implements IRequest {
   private String state;

   public abstract <T> Result<?> handle(String Url, HState state, Map<String, String> head, Map<String ,String > pathValue, T body, NetTool tool);


   @Override
   public <T> Result<?> request(String Url, HState state, Map<String, String> head,Map<String ,String > pathValue,T body){
      Result<?> result = null;
      try {
         if (pathValue != null){
            Url = this.setPathValue(Url,pathValue);
         }

         URL url = new URL(Url);
         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
         connection.setRequestMethod(state.getState());

         connection.setRequestProperty("Content-Type","application/json");
         if (head != null){

            setHead(head, connection);
         }

         if (body != null){
            setBody(body, connection);
         }

         connection.setConnectTimeout(300);
         connection.connect();

         int code = connection.getResponseCode();
         if (code == 200){
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuilder content = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
               content.append(inputLine);
            }
            result = JSON.parseObject(content.toString(),Result.class);
         }

      } catch (IOException e) {
         e.printStackTrace();
      }
      this.state = null;
      return result;
   }

   protected void setHead(Map<String ,String > head,HttpURLConnection connection){
      for (String s : head.keySet()) {
         connection.setRequestProperty(s,head.get(s));
      }
   }

   protected <T> void setBody(T body,HttpURLConnection connection) throws IOException {
      connection.setDoOutput(true);
      String json = JSON.toJSONString(body);
      DataOutputStream out = new DataOutputStream(connection.getOutputStream());
      out.writeBytes(json);
      out.flush();
      out.close();
   }

   protected String setPathValue(String url,Map<String,String> map){
      StringBuilder urlBuilder = new StringBuilder(url+"?");
      Object[] keys = map.keySet().toArray();

      for (int i = 0; i < keys.length; i++) {
         String key = (String) keys[i];
         String parameters = key+"="+map.get(key);
         if (i != keys.length-1){
            parameters = parameters+"&";
         }
         urlBuilder.append(parameters);
      }
      url = urlBuilder.toString();
      return url;
   }
}
